import 'dart:io';

import 'package:flutter/material.dart';
import 'package:core_package/core_package.dart';
import 'package:flutter_app_unionad/core/withdraw_deposit_manager/withdraw_deposit_request.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'h5_generate_contract_model.dart';

class ContractPage extends StatefulWidget {
  static const routeName = "/contractPage";
  final H5GenerateContractModel? data;

  ContractPage({this.data});

  @override
  _ContractPageState createState() => _ContractPageState();
}

class _ContractPageState extends State<ContractPage> {
  int index = 0;
  H5GenerateContractModel? model;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WithdrawDepositRequest.getH5GenerateContract().then((value) {
      if (mounted)
        setState(() {
          model = value;
        });
    });
  }

  void dd(WebViewController vc) async {
    final f = await vc.currentUrl();
    print(f);
  }

  @override
  Widget build(BuildContext context) {

    if (Platform.isAndroid) {
      if (model == null) {
        return Scaffold(
          appBar: MyAppBar(context, title: "合同签约"),
          body: ProgressDialog.showFirstLoading(),
        );
      }
      return WebviewScaffold(
        appBar: MyAppBar(context, title: "合同签约"),
        url: model!.url!,
        withJavascript: true,
      );
    }
    return Scaffold(
      appBar: MyAppBar(context, title: "合同签约"),
      body: IndexedStack(
        index: index,
        children: [
          Center(
            child: ProgressDialog.showFirstLoading(),
          ),
          model == null
              ? Container()
              : WebView(
                  javascriptMode: JavascriptMode.unrestricted,
                  initialUrl: model!.url,
                  // initialMediaPlaybackPolicy: AutoMediaPlaybackPolicy.always_allow,
                  onWebViewCreated: (controller) {
                    dd(controller);
                  },
                  onPageFinished: (url) {
                    if (mounted)
                      setState(() {
                        index = 1;
                      });
                  },
                ),

//          Center(
//            child: a.ErrorWidget(),
//          )
        ],
      ),
    );
  }
}
